﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Globalization;

namespace HREDU.Hoang_Tuan
{
    public enum enumNHOM 
    {
        DONVI = 1,
        CHUCVU = 2
    }

    public enum enumDANHMUC 
    {
        DANTOC, MONDAOTAO, HOCHAM, THANHPHAN, 
        SUCKHOE, TRINHDOCHUYENMON, TRINHDO, 
        VANHOA, TONGIAO, BACHOC, HEDAOTAO, DANHHIEU
    }

    public struct TINHTHANH
    {
        public string Tinh;
        public string Huyen;
        public string Xa;
        public string XaHuyenTinh;
    }

    public class GeneralCode
    {
        internal string GetConnType(string sConnString)
        {
            string sGetConnType = "";
            if (sConnString.IndexOf("Provider=SQLOLEDB") > -1)
            {
                sGetConnType = "SQL";
            }
            else
            {
                sGetConnType = "OLEDB";
            }

            return sGetConnType;
        }

        ////Ham nay dung de doc chuoi ket noi SQL de dung cho MITI.BaseDB.Instance.g_objConnFactory
        //internal string GetCnnStrFromFile(string pFileSpec)
        //{
        //    string mCnnStr = "";
        //    try
        //    {
        //        StreamReader sr = new StreamReader(pFileSpec);
        //        string line;
        //        do
        //        {
        //            line = sr.ReadLine();
        //            if ((line != null))
        //            {
        //                if (line.IndexOf("Provider") > -1)
        //                {
        //                    mCnnStr = line;
        //                    break; // TODO: might not be correct. Was : Exit Do
        //                }
        //            }
        //        }
        //        while (!(line == null));
        //        sr.Close();
        //    }
        //    catch (Exception e)
        //    {
        //        throw e;
        //    }

        //    return mCnnStr;
        //}

        public static string FormatMoneyStyle()
        {
            return "#,##0.##";
        }

        public static string FormatQuantityStyle()
        {
            return "#,##0.##";
        }

        public static CultureInfo GetCultureInfoUS()
        {
            CultureInfo cul  =new CultureInfo("en-US", false);
            cul.NumberFormat.NumberDecimalSeparator = ".";
            cul.NumberFormat.NumberGroupSeparator = ",";
            cul.DateTimeFormat.ShortDatePattern = "M/d/yyyy";
            cul.DateTimeFormat.LongDatePattern = "dddd, MMMM dd, yyyy";
            return cul;
        }

        public static bool IsSoChan(double pValue)
        {
            bool Kq = false;
            string strValue = pValue.ToString("000000000000000.00");

            string strPhanNguyen = strValue.Substring(0, 15);
            string strPhanThapPhan = strValue.Substring(16, 2);

            if (double.Parse(strPhanThapPhan) == 0)
            {
                Kq = true;
            }

            return Kq;
        }

        //Doan code nay dung de set width cua column cua Datagrid va xuat ra file
        public static void OutputText(Janus.Windows.GridEX.GridEX gridObject)
        {

            FileInfo t = new FileInfo("CodeOutput.txt");
            StreamWriter Tex = t.CreateText();
            string str = "";
            foreach (Janus.Windows.GridEX.GridEXColumn column in gridObject.RootTable.Columns)
            {
                str = gridObject.Name + ".RootTable.Columns[\"" + column.Key + "\"].Width =" + column.Width + ";";
                Tex.WriteLine(str);
                Tex.Write(Tex.NewLine);

            }
            Tex.Close();
        }

        //Doan code nay dung de set width cua column cua Datagrid va xuat ra file
        public static void OutputText(DataGridView gridObject)
        {

            FileInfo t = new FileInfo("CodeOutput.txt");
            StreamWriter Tex = t.CreateText();
            string str = "";
            //foreach ( column in gridObject.RootTable.Columns)
            //{
            //    str = gridObject.Name + ".RootTable.Columns[\"" + column.Key + "\"].Width =" + column.Width + ";";
            //    Tex.WriteLine(str);
            //    Tex.Write(Tex.NewLine);

            //}
            Tex.Close();
        }

        #region "Doi So Ra Chu"

        public string docchu(int so1)
        {

            int tram;
            int chuc;
            int dv;
            int so;
            string[] chu = new string[10];
            string chuoi1;
            string chuoi2;
            string chuoi;
            so = so1;
            chu[0] = "không";
            chu[1] = "một";
            chu[2] = "hai";
            chu[3] = "ba";
            chu[4] = "bốn";
            chu[5] = "năm";
            chu[6] = "sáu";
            chu[7] = "bảy";
            chu[8] = "tám";
            chu[9] = "chín";
            tram = so / 100;
            so = so % 100;
            chuc = so / 10;
            dv = so % 10;
            switch (chuc)
            {
                case 0:
                    chuoi2 = (tram == 0 ? "" : (dv == 0 ? "" : "lẻ"));
                    break;
                case 1:
                    chuoi2 = "mười";
                    break;
                default:
                    chuoi2 = chu[chuc] + " mươi";
                    break;
            }
            switch (tram)
            {
                case 0:
                    chuoi1 = "";
                    break;
                default:
                    chuoi1 = chu[tram] + " trăm ";
                    break;
            }
            switch (dv)
            {
                case 0:
                    chuoi = "";
                    break;
                case 1:
                    chuoi = ((chuoi2 == "") || (chuoi2 == "mười") || (chuoi2 == "lẻ")) ? "một" : "mốt";
                    break;
                case 5:
                    chuoi = ((chuoi2 == "lẻ") || (chuoi2 == "")) ? "năm" : "lăm";
                    break;
                default:
                    chuoi = chu[dv];
                    break;
            }
            string str = chuoi1 + (chuoi2 != "" ? chuoi2 : "") + " " + chuoi;
            return str.Trim();
        }

        public string doc(double so1)
        {
            string chuoi1;
            string chuoi2;
            string chuoi3;
            string chuoi4;
            chuoi1 = "";
            if (so1 == 0)
                return "không";
            double so;
            so = so1;
            int ty;
            int trieu;
            int ngan;
            int dv;
            double rr;
            ty = (int)(so / 1000000000);
            rr = ty;
            so = so - (double)1000000000 * (double)ty;
            trieu = (int)(so / 1000000);
            so = so % 1000000;
            ngan = (int)(so / 1000);
            dv = (int)(so % 1000);
            if (ty > 1000)
            {
                chuoi1 = doc(rr) + " tỷ";
            }
            else if (ty > 0)
            {
                chuoi1 = docchu(ty) + " tỷ";
            }
            chuoi2 = (trieu > 0 ? docchu(trieu) + " triệu" : "");
            chuoi3 = (ngan > 0 ? docchu(ngan) + " ngàn" : "");
            chuoi4 = (dv > 0 ? docchu(dv) : "");
            return chuoi1 + (chuoi2 != "" ? " " + chuoi2 : "") + (chuoi3 != "" ? " " + chuoi3 : "") + (chuoi4 != "" ? " " + chuoi4 : "");
        }

        public object DoiSoRaChu(double so)
        {
            double so1, le;
            string chuoi;
            chuoi = so.ToString("000000000000000000.00");
            so1 = double.Parse(chuoi.Substring(0, chuoi.Length - 3));
            le = double.Parse(chuoi.Substring(chuoi.Length - 2));

            if (le > 0)
            {
                if (le < 10)
                {
                    chuoi = doc(so1) + " phẩy không " + doc(le);
                }
                else
                {
                    chuoi = doc(so1) + " phẩy " + doc(le);
                }
            }
            else
            {
                chuoi = doc(so1);
            }
            if ((chuoi.Length != 0))
            {
                string c;
                c = chuoi.Substring(0, 1);
                chuoi = c.ToUpper() + chuoi.Substring(1);
            }
            return chuoi + " đồng";
        }

        #endregion

    }
    
}
